package com.warm.common.image;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Description 测试画图
 * @Author iyb-liangchuang
 * @Date 2021/7/8 17:56
 */
public class JFreeChartUtil2 {

    public static void main(String[] args) throws IOException {
        int sum = 50;//随机生成20对数
        Map<Double, Double> map1 = new HashMap<>();
        Map<Double, Double> map2 = new HashMap<>();
        double[] restX = getRandomDouble(1, 200, 1, sum);
        double[] restY = getRandomDouble(1, 200, 1, sum);
        for (int i = 0; i < sum; i++) {
            map1.put(restX[i], restY[i]);
            map2.put(restY[i], restX[i]);
        }

        Map<Double, Double>[] dataSet = new Map[]{map1, map2};
        String[] types = new String[]{"A", "B"};
        //调用绘图工具
        JFreeChartUtil2.drawLineChart( "测试", "X轴",
                "Y轴", dataSet, types);
    }

    public static double[] getRandomDouble(int start, int end, int retain, int sum) {
        double[] res = new double[sum];

        for (int i = 0; i < sum; i++) {
            BigDecimal db = new BigDecimal(Math.random() * (end - start) + start);
            //四舍五入
            res[i] = db.setScale(retain, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return res;
    }

    private String titleFont;
    private int titleFontSize;
    private String xyFont;
    private int xyFontSize;

    public JFreeChartUtil2() {
        this.titleFont = "微软雅黑";
        this.titleFontSize = 20;
        this.xyFont = "微软雅黑";
        this.xyFontSize = 15;
    }



    /**
     * @param chartTitle  图标题
     * @param xName       x轴命名
     * @param yName       y轴命名
     * @param dataSet     数据集
     * @param types       线条种类
     */
    public static void drawLineChart(String chartTitle,
                                     String xName,
                                     String yName,
                                     Map<Double, Double>[] dataSet,
                                     String[] types) throws IOException {
        JFreeChartUtil2 tools = new JFreeChartUtil2();
        IntervalXYDataset dataset = tools.getLineDataset(dataSet, types);
        JFreeChart chart = tools.getLineChart(chartTitle, xName, yName, dataset);

        //绘图模式化
        tools.setChartCSS(chart);
        String name = "D:\\" + System.currentTimeMillis() + ".jpg";
        ChartUtilities.saveChartAsJPEG(new File(name), chart, 900, 600);
    }

    private JFreeChart getLineChart(String title, String xName, String yName, XYDataset dataset) {
        /**
         * 图标标题，x轴名称，y轴名称，数据集合，图标显示方向，是否使用图示，是否生成工具栏，是否生成URL链接
         */
        JFreeChart chart = ChartFactory.createXYLineChart(
                title,
                xName,
                yName,
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
        );
        return chart;
    }


    /**
     * 自定义设置图表字体样式
     *
     * @param chart
     */
    private void setChartCSS(JFreeChart chart) {
        //初始化
        chart.setBackgroundPaint(ChartColor.WHITE);
        XYPlot plot = chart.getXYPlot();

        //标题
        TextTitle textTitle = chart.getTitle();
        textTitle.setFont(new Font(titleFont, Font.BOLD, titleFontSize));
        LegendTitle legendTitle = chart.getLegend();
        legendTitle.setItemFont(new Font(titleFont, Font.PLAIN, titleFontSize));


        //图表xy轴字体设置
        plot.getDomainAxis().setLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));
        plot.getDomainAxis().setTickLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));
        plot.getRangeAxis().setTickLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));
        plot.getRangeAxis().setLabelFont(new Font(xyFont, Font.PLAIN, xyFontSize));

        //设置背景色-xy轴格子色
        plot.setBackgroundPaint(ChartColor.WHITE);
        plot.setRangeGridlinePaint(ChartColor.lightGray);
//        plot.setDomainGridlinePaint(ChartColor.lightGray);

        //折线图渲染
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        plot.setRenderer(renderer);
//        renderer.setPaint(ChartColor.BLACK);
        chart.getLegend().setPosition(RectangleEdge.RIGHT);

    }

    /**
     * @param dataSets int:double
     * @param types    折线的种类
     * @return
     */
    private IntervalXYDataset getLineDataset(Map<Double, Double>[] dataSets, String[] types) {

        XYSeriesCollection dataSet = new XYSeriesCollection();
        int index = 0;
        for (String type : types) {
            XYSeries series = new XYSeries(type);
            for (Map.Entry<Double, Double> data : dataSets[index++].entrySet()) {
                series.add(data.getKey(), data.getValue());
            }
            dataSet.addSeries(series);
        }
        return dataSet;
    }
}
